/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ua.epam.muzalevskiy.third_project.XMLParsers;

import com.sun.org.apache.xerces.internal.parsers.DOMParser;
import java.io.IOException;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

/**
 * This class realized validation of XML file for accordance to XML Schema.
 *
 * @author Muzalevskiy Vitaliy
 */
public class Validator {

    /**
     * This method validate XML file full name of which entered as parameter.
     *
     * @param filename
     */
    public void validate(String filename) {

        DOMParser parser = new DOMParser();

        parser.setErrorHandler(new ErrorHandler() {
            @Override
            public void warning(SAXParseException exception) throws SAXException {
                System.out.println(exception.getMessage());
            }

            @Override
            public void error(SAXParseException exception) throws SAXException {
                System.err.println(exception.getMessage());
            }

            @Override
            public void fatalError(SAXParseException exception) throws SAXException {
                System.err.println(exception.getMessage());
            }
        });
        try {
            parser.setFeature("http://xml.org/sax/features/validation", true);
            parser.setFeature("http://apache.org/xml/features/validation/schema", true);
            parser.parse(filename);

        } catch (SAXException | IOException ex) {
            System.err.println(ex.getMessage());
        }
    }
}
